//
//  GSVistorView.swift
//  SinaWeiboDay1
//
//  Created by SamuelChan on 16/4/8.
//  Copyright © 2016年 SamuelChan. All rights reserved.
//

import UIKit
protocol GSVistorViewDelegate :NSObjectProtocol{
    func vistorViewClickLoginBtn()
    
    func vistorViewClickRegisterBtn()

}
class GSVistorView: UIView {
    weak var delegate : GSVistorViewDelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        startRotation()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 自定义的私有方法
    private func setUI (){
        addSubview(iconView)
        addSubview(converView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        converView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        //添加约束
        setLayoutConstraint()
    }
    
    private func setLayoutConstraint() {
        
        
        //转轮
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -40))
        //遮罩conner
        addConstraint(NSLayoutConstraint(item: converView, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: converView, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: converView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: converView, attribute: .Bottom, relatedBy: .Equal, toItem: registerButton, attribute: .Bottom, multiplier: 1, constant: -10))

        //小房子
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //消息
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 300))
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: -80))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 76))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 46))
        
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 80))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 76))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 46))

    }
    //让转轮开始旋转
    private func startRotation() {
        let animation = CABasicAnimation(keyPath:"transform.rotation")
        animation.toValue = M_PI * 2
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false

        /**将核心动画添加到图层上*/
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    //让外界来决定显示的内容(共有的)
    func switchView (text text : String,pic:String){
        homeView.hidden = true
        converView.hidden = true
        iconView.layer.removeAllAnimations()
        messageLabel.text = text
        iconView.image = UIImage(named:pic)
        
    }
    
    @objc private func clickloginBtn(){
        delegate?.vistorViewClickLoginBtn()
    }
    
    @objc private func clickRegistBtn(){
        delegate?.vistorViewClickRegisterBtn()
        
    }

    
    //MARK: - 懒加载
    //转轮
    lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩
    lazy var converView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    lazy var homeView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //uilabel
    lazy var messageLabel : UILabel = {
       let label = UILabel()
        label.text = "关注一些人,回来看看有什么惊喜"
        label.numberOfLines = 0
        label.textAlignment = .Center
//        label.backgroundColor = UIColor.grayColor()
        label.font = UIFont.systemFontOfSize(18)
        return label
    }()
    //registerButton
    lazy var registerButton:UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.addTarget(self, action: Selector("clickRegistBtn"), forControlEvents: .TouchUpInside)

        return button
    } ()
    //loginOnButton
    lazy var loginButton:UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.addTarget(self, action: Selector("clickloginBtn"), forControlEvents: .TouchUpInside)
        return button
    }()

}
